#!/usr/bin/python
# coding=utf8

#
# http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
#

import re

def re_toy(string,pattern):
    p = re.compile(pattern)
    try:    
        subs = p.sub(r'**\1**\2',string),
    except:
        subs = ""

    def func(m):
        try:
            return m.group(1).upper() + ' * ' + m.group(2).title()
        except:
            return ''

    return dict(
            pattern = p,
            match = p.match(string),
            search = p.search(string),
            split = p.split(string),
            findall = p.findall(string),
            finditer = p.finditer(string),
            subf = p.sub(func,string),
            subs = subs,
            )

data = [
        ('hellore1 hellore2',r'hellore'),
        ('hello world!',r'(\w+)(\w+)(?P<duoduo>.*)'),
        ('i say, hello world!',r'(\w+) (\w+)'),
        ('oasdf1bdd2c2d3',r'\d+'),
        ]

redict = [re_toy(string=d[0],pattern=d[1]) for d in data]
t = redict[2]
